class DuchiesController < ApplicationController
  before_filter :login_required, :except => [:show]
  
  def authorized?(user = current_player())
    return user.admin?
  end
  
  # GET /duchies
  # GET /duchies.xml
  def index
    @duchies = Duchy.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @duchies }
    end
  end

  # GET /duchies/1
  # GET /duchies/1.xml
  def show
    @duchy = Duchy.find(params[:id])
    @title = Title.new
    @title.character_id = player_character.id unless player_character.nil?
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @duchy }
    end
  end

  # GET /duchies/new
  # GET /duchies/new.xml
  def new
    @duchy = Duchy.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @duchy }
    end
  end

  # GET /duchies/1/edit
  def edit
    @duchy = Duchy.find(params[:id])
  end

  # POST /duchies
  # POST /duchies.xml
  def create
    @duchy = Duchy.new(params[:duchy])

    respond_to do |format|
      if @duchy.save
        flash[:notice] = 'Duchy was successfully created.'
        format.html { redirect_to(@duchy) }
        format.xml  { render :xml => @duchy, :status => :created, :location => @duchy }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @duchy.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /duchies/1
  # PUT /duchies/1.xml
  def update
    @duchy = Duchy.find(params[:id])

    respond_to do |format|
      if @duchy.update_attributes(params[:duchy])
        flash[:notice] = 'Duchy was successfully updated.'
        format.html { redirect_to(@duchy) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @duchy.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /duchies/1
  # DELETE /duchies/1.xml
  def destroy
    @duchy = Duchy.find(params[:id])
    @duchy.destroy

    respond_to do |format|
      format.html { redirect_to(duchies_url) }
      format.xml  { head :ok }
    end
  end
end
